<template>
  <div class="app-container">
    <!-- 移动端骨架屏 -->
    <mobile-skeleton 
      v-if="isMobile && loading" 
      :showHeader="true"
      :showCards="true"
      :cardsCount="3"
      :showPagination="false"
    />
    
    <div v-else>
      <h5>
        <img
          :src="backImg"
          style="width:25px;height:25px;cursor:pointer"
          @click="handleBack"
        >
      </h5>
      <h2 class="text-center">{{ detailData.name }}</h2>
      <p class="text-center" style="color: #666">{{ detailData.create_time }}</p>

      <el-row :gutter="24" style="margin-top: 50px">
        <el-col :span="4" class="text-center"> 考试时长：{{ detailData.time_limit }}分钟 </el-col>
        <el-col :span="4" class="text-center">
          试卷总分：{{ detailData.total_score }}分
        </el-col>
        <el-col :span="4" class="text-center">
          及格分数：{{ detailData.qualify_score }}分
        </el-col>

        <el-col :span="4" class="text-center">
          考试类型：{{ detailData.type | ExamTypeFormat }}
        </el-col>
        <el-col :span="4" class="text-center">
          关联课程：{{ detailData.course }}
        </el-col>
      </el-row>

      <el-card style="margin-top: 20px">
        <div v-for="(item, index) in detailData.question" :key="item.id" class="qu-content">
          <!-- 题目 -->
          <p v-html="index + 1 + '.' + item.stem" class="detailData_title" />
          <p v-if="item.image != null && item.image != ''">
            <el-image :src="item.image" style="max-width: 100%" />
          </p>
          <!-- 单选 -->
          <div v-if="item.type == 1">
            <el-radio-group v-model="item.value[0]">
              <el-radio v-for="an in item.option" :label="an.id" :key="an.id">
                {{ an.name }}.{{ an.content }}
                <div v-if="an.image != null && an.image != ''" style="clear: both">
                  <el-image :src="an.image" style="max-width: 100%" />
                </div>
              </el-radio>
            </el-radio-group>

            <el-row :gutter="24">
              <el-col :span="12" style="color: #24da70">
                正确答案：{{ item.radioRight }}
              </el-col>
            </el-row>
          </div>
          <!-- 判断题 -->
          <div v-if="item.type == 3">
            <el-radio-group v-model="item.value">
              <el-radio :label="1">对 </el-radio>
              <el-radio :label="0">错 </el-radio>
            </el-radio-group>
            <el-row :gutter="24">
              <el-col :span="12" style="color: #24da70">
                正确答案：{{ item.answer | TrueFalesefilter }}
              </el-col>
            </el-row>
          </div>
          <!-- 问答题 -->
          <div v-if="item.type == 4">
            <el-row :gutter="24">
              <el-col :span="12"> 参考答案：{{ item.answer }} </el-col>
            </el-row>
          </div>
          <!-- 多选题 -->
          <div v-if="item.type == 2">
            <el-checkbox-group v-model="item.value">
              <el-checkbox v-for="an in item.option" :key="an.id" :label="an.id">{{ an.name }}.{{ an.content }}
                <div v-if="an.image != null && an.image != ''" style="clear: both">
                  <el-image :src="an.image" style="max-width: 100%" />
                </div>
              </el-checkbox>
            </el-checkbox-group>

            <el-row :gutter="24">
              <el-col :span="12" style="color: #24da70">
                正确答案：{{ multiRights[item.id].join('、') }}
              </el-col>
            </el-row>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { examDetail } from "@/api/vadmin/exam/exam";
import backImg from "@/assets/images/back.png";
import { mapGetters, mapState } from "vuex";
import variables from "@/assets/styles/variables.scss";
import MobileSkeleton from "@/components/MobileSkeleton";
import "@/assets/styles/mobile/exam.scss";

export default {
  name: "showExam",
  components: { MobileSkeleton },
  data() {
    return {
      backImg: backImg,
      // 试卷ID
      paperId: "",
      loading: true,
      detailData: {
        quList: [],
      },
      radioValues: {},
      multiValues: {},
      radioRights: {},
      multiRights: {},
      myRadio: {},
      myMulti: {},
      isMobile: false,
      mediaQueryList: null,
    };
  },
  computed: {
    ...mapState(["settings"]),
    variables() {
      return variables;
    },
    theme() {
      return this.$store.state.settings.theme;
    },
  },
  filters: {
    TrueFalesefilter(val) {
      if (val == 0) {
        return "错";
      } else {
        return "对";
      }
    },
    ExamTypeFormat(val) {
      if (val == 1) {
        return "单元测试";
      } else if (val == 2) {
        return "章节测试";
      } else if (val == 3) {
        return "课程测试";
      } else if (val == 4) {
        return "综合测试";
      } else {
        return "";
      }
    },
  },
  created() {
    const id = this.$route.params.id;
    if (typeof id !== "undefined") {
      this.paperId = id;
      this.fetchData(id);
    }
  },
  mounted() {
    // 使用媒体查询API直接监听视口变化
    this.mediaQueryList = window.matchMedia('(max-width: 767px)');
    
    // 初始化isMobile状态
    this.isMobile = this.mediaQueryList.matches;
    
    // 为媒体查询添加监听器
    const handleMediaChange = (e) => {
      this.isMobile = e.matches;
      
      // 强制组件重新渲染
      this.$forceUpdate();
    };
    
    // 使用适当的API添加监听器
    if (this.mediaQueryList.addEventListener) {
      this.mediaQueryList.addEventListener('change', handleMediaChange);
    } else {
      // 旧版浏览器支持
      this.mediaQueryList.addListener(handleMediaChange);
    }
    
    // 存储监听器以便后续移除
    this.mediaChangeHandler = handleMediaChange;
  },
  
  beforeDestroy() {
    // 移除媒体查询监听器
    if (this.mediaQueryList) {
      if (this.mediaQueryList.removeEventListener) {
        this.mediaQueryList.removeEventListener('change', this.mediaChangeHandler);
      } else {
        // 旧版浏览器支持
        this.mediaQueryList.removeListener(this.mediaChangeHandler);
      }
    }
  },
  methods: {
    handleBack() {
      this.$router.back()//返回浏览器上一页，并保留上一页数据
    },
    fetchData(id) {
      examDetail(id).then((response) => {
        // 试卷内容
        this.detailData = response;
        this.detailData.course = response.name;
        // 填充该题目的答案
        this.detailData.question.forEach((item) => {
          const multiRight = [];
          const myMulti = [];

          item.option.forEach((an) => {
            if (item.type == 1) {
              //单选题
              if (item.answer[0] == an.id) {
                item.radioRight = an.name; //正确选择
              }
            } else if (item.type == 2) {
              //多选题
              // item.answer.push(an.id)
              // item.value.push(an.abc)

              item.answer.forEach((a) => {
                if (a == an.id) {
                  multiRight.push(an.name);
                }
              });
            }
            this.multiRights[item.id] = multiRight;
          });
        });
        this.loading = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.qu-content {
  border-bottom: #eee 1px solid;
  padding-bottom: 10px;
}

.qu-content div {
  line-height: 30px;
}

.el-checkbox-group label,
.el-radio-group label {
  width: 100%;
}

.card-title {
  background: #eee;
  line-height: 35px;
  text-align: center;
  font-size: 14px;
}

.card-line {
  padding-left: 10px;
}

.card-line span {
  cursor: pointer;
  margin: 2px;
}

.detailData_title {
  display: flex;
  align-items: center;
}

.text-center {
  text-align: center;
}
</style>